/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NhanLichChuyenBayFrm.java
 *
 * Created on May 28, 2011, 2:23:19 AM
 */
package gui;

import dao.ChuyenBayDAO;
import dao.ThamSoDAO;
import dao.TuyenBayDAO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.ChuyenBayPOJO;
import pojo.ThamSoPOJO;
import pojo.TuyenBayPOJO;

/**
 *
 * @author b2
 */
public class NhanLichChuyenBayFrm extends javax.swing.JFrame {

    private boolean kq;

    /** Creates new form NhanLichChuyenBayFrm */
    public NhanLichChuyenBayFrm() {
        initComponents();
    }

    public void initNhanLichChuyenBay() {
        ArrayList<TuyenBayPOJO> ds = TuyenBayDAO.LayDanhSachTuyenBay();
        String[] dsTuyenBay = new String[ds.size()];

        for (int i = 0; i < ds.size(); i++) {
            TuyenBayPOJO tb = ds.get(i);
            dsTuyenBay[i] = tb.getMaTuyenBay();
        }
        DefaultComboBoxModel dcm = new DefaultComboBoxModel(dsTuyenBay);
        this.jcbTuyenBay.setModel(dcm);

        //
        Calendar calendar = new GregorianCalendar();
        Date curDate = calendar.getTime();
        this.jdcNgayGio.setDate(curDate);
        this.jdcNgayGio.setMinSelectableDate(curDate);
        this.txtSoLuongGheHang1.setText("");
        this.txtSoLuongGheHang2.setText("");
        this.txtThoiGianBay.setText("");
        //set and get QD1
    }

    public void themChuyenBay() {
        String maChuyenBay = this.txtMaChuyenBay.getText();
        String maTuyenBay = (String) this.jcbTuyenBay.getSelectedItem();
        String cbgio = (String) this.jcbCBGio.getSelectedItem();
        String cbPhut = (String) this.jcbCBPhut.getSelectedItem();

        Date ngaygiof = this.jdcNgayGio.getDate();
        ngaygiof.setHours(Integer.parseInt(cbgio));
        ngaygiof.setMinutes(Integer.parseInt(cbPhut));
        ngaygiof.setSeconds('0');

        String patten = "MM/dd/yyyy hh:mm:ss a";
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        String ngaygio = sdf.format(ngaygiof);

        int thoigianbay = Integer.parseInt(this.txtThoiGianBay.getText());
        int slg1 = Integer.parseInt(this.txtSoLuongGheHang1.getText());
        int slg2 = Integer.parseInt(this.txtSoLuongGheHang2.getText());
//------------
        //Date abc = null;
        //abc.toString();
        ChuyenBayPOJO cb = new ChuyenBayPOJO();
        cb.setMaChuyenBay(maChuyenBay);
        cb.setMaTuyenBay(maTuyenBay);
        cb.setNgayGio(ngaygio);
        System.out.println(ngaygiof.toString());
        cb.setThoiGianBay(thoigianbay);
        cb.setSLG1(slg1);
        cb.setSLG2(slg2);
        boolean kqua = ChuyenBayDAO.themChuyenBay(cb);
        if (kqua == true) {

            int soGhe = slg1 + slg2;
            ChuyenBayDAO.themTinhTrangVe(maChuyenBay, soGhe, 0);
            int themCt = JOptionPane.showConfirmDialog(this, "Thêm chi tiết chuyến bay", "Thông báo", JOptionPane.YES_NO_OPTION);
            if (themCt == JOptionPane.YES_OPTION) {
                ThemChiTietChuyenBayFrm frm = new ThemChiTietChuyenBayFrm(this.txtMaChuyenBay.getText());
                frm.setVisible(true);
            }
            //this.themChiTietChuyenBay();
        } else {
            JOptionPane.showMessageDialog(this, "Thêm thất bại");
        }
    }

    public boolean kiemtraQD() {
        boolean qd = true;
        //kiemtra input
        if (Integer.parseInt(this.txtSoLuongGheHang2.getText()) < 0 || Integer.parseInt(this.txtSoLuongGheHang1.getText()) < 0) {
            JOptionPane.showMessageDialog(this, "Kiểm tra lại giá trị nhập vào");
            return false;
        }
        ThamSoPOJO ts = ThamSoDAO.layDanhSahThamSO();
        int tgBay = Integer.parseInt(this.txtThoiGianBay.getText());
        if (tgBay < ts.getThoiGianBayToiThieu()) {
            JOptionPane.showMessageDialog(this, "Sai quy định về thời gian bay ");
            qd = false;
        }
        return qd;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMaChuyenBay = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtSoLuongGheHang1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jcbTuyenBay = new javax.swing.JComboBox();
        txtSoLuongGheHang2 = new javax.swing.JTextField();
        btnThemMoi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jdcNgayGio = new com.toedter.calendar.JDateChooser();
        jcbCBGio = new javax.swing.JComboBox();
        jcbCBPhut = new javax.swing.JComboBox();
        txtThoiGianBay = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nhận lịch chuyến bay");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("NHẬN LỊCH CHUYẾN BAY");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nhận lịch chuyến bay"));

        jLabel2.setText("Mã chuyến bay");

        jLabel3.setText("Số lượng ghế hạng 2");

        jLabel4.setText("Tuyến bay");

        jLabel5.setText("Ngày giờ");

        jLabel6.setText("Thời gian bay");

        jLabel7.setText("Số lượng ghế hạng 1");

        btnThemMoi.setText("Thêm mới");
        btnThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemMoiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jdcNgayGio.setDateFormatString("MM/dd/yyyy");

        jcbCBGio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));

        jcbCBPhut.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));
        jcbCBPhut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbCBPhutActionPerformed(evt);
            }
        });

        jLabel9.setText("phút");

        jLabel12.setText("Giờ");

        jLabel13.setText("Phút");

        jButton1.setText("Thêm chi tiết");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(37, 37, 37)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jdcNgayGio, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                                    .addComponent(txtSoLuongGheHang1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                                    .addComponent(txtMaChuyenBay, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                                    .addComponent(txtThoiGianBay, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                                .addGap(6, 6, 6)
                                .addComponent(jLabel9))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jButton1)
                                .addGap(56, 56, 56)
                                .addComponent(btnThemMoi)
                                .addGap(9, 9, 9)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel4)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel12)
                                        .addGap(18, 18, 18)
                                        .addComponent(jcbCBGio, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtSoLuongGheHang2)
                                            .addComponent(jcbTuyenBay, 0, 100, Short.MAX_VALUE)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(1, 1, 1)
                                        .addComponent(jLabel13)
                                        .addGap(18, 18, 18)
                                        .addComponent(jcbCBPhut, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(50, 50, 50)
                                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMaChuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jcbTuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jdcNgayGio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel12)
                        .addComponent(jcbCBGio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel13)
                        .addComponent(jcbCBPhut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel3)
                    .addComponent(txtSoLuongGheHang1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoLuongGheHang2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtThoiGianBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btnThoat)
                    .addComponent(btnThemMoi))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(150, 150, 150)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initNhanLichChuyenBay();
    }//GEN-LAST:event_formWindowOpened

    private void btnThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemMoiActionPerformed
        // TODO add your handling code here:
        boolean ktQD = this.kiemtraQD();
        if(ktQD == true){
            this.themChuyenBay();
        }

    }//GEN-LAST:event_btnThemMoiActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void jcbCBPhutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbCBPhutActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbCBPhutActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String maCB = this.txtMaChuyenBay.getText();
        ThemChiTietChuyenBayFrm frm = new ThemChiTietChuyenBayFrm(maCB);
        frm.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NhanLichChuyenBayFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox jcbCBGio;
    private javax.swing.JComboBox jcbCBPhut;
    private javax.swing.JComboBox jcbTuyenBay;
    private com.toedter.calendar.JDateChooser jdcNgayGio;
    private javax.swing.JTextField txtMaChuyenBay;
    private javax.swing.JTextField txtSoLuongGheHang1;
    private javax.swing.JTextField txtSoLuongGheHang2;
    private javax.swing.JTextField txtThoiGianBay;
    // End of variables declaration//GEN-END:variables
}
